<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RPC services</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
	<div>
		<table border="1" cellspacing="0">
			<thead>
				<tr>
					<td width="2%">序号</td>
					<td width="16%">方法名</td>
					<td width="30%">参数</td>
					<td width="2%">调试</td>
					<td width="50%">结果</td>
				</tr>
			</thead>
			<tbody id="tbody">
			</tbody>
		</table>
	</div>
</body>
<script type="text/javascript">
init();

function init() {
	/* localhostPath = 'http://localhost:9999'; */
	localhostPath = getLocalhostPaht();
	load();
}

var localhostPath;

function getLocalhostPaht() {
	var curWwwPath = window.document.location.href;
	var pos = curWwwPath.lastIndexOf("/");
	curWwwPath = curWwwPath.substring(0, pos);
	pos = curWwwPath.lastIndexOf("/");
	curWwwPath = curWwwPath.substring(0, pos);
	return curWwwPath;
} 

//或者服务配置
function getServersConf() {
	var result = post(localhostPath + "/Boot/getServiceConfigurations",{});
	if(result.resultstate == 1)
		return result.result;
	else {
		alert(JSON.stringify(result));
		return;
	}
}

function getParams(conf, index) {
	var paras = conf.paramNames;
	var text = "";
    for(var i = 0; i<paras.length; i++) {
    	var name = paras[i];
    	var id = index+name;
    	text += name+":<input name=\""+name+"\" id=\""+id+"\"></br>"
    }
	return text;
}

var confCache;
//加载配置
function load() {
	confCache = getServersConf();
	var result = confCache;
	var body = "";
	var i = 0;
	for(var key in result) {
		body +="<tr>"
			 +"<td>"+i+"</td>"
			 +"<td>"+key+"</td>"
			 +"<td>"+getParams(result[key], key)+"</td>"
			 +"<td><a href=\"javascript:;\" id=\""+key+"\" onclick=\"debugMethod(this)\">调试</a></td>"
			 +"<td><span id=\""+key+"result\"></span></td>"
			+"</tr>"
		i++;
	}
	$("#tbody").html(body);
}

function debugMethod(thisDoc) {
	var key = thisDoc.id;
	var paras = confCache[key].paramNames;
	var text = {};
    for(var i = 0; i<paras.length; i++) {
    	var name = paras[i];
    	var nameId = key+name;
    	text[name]=document.getElementById(nameId).value;
    }
	var rel = post(localhostPath+"/"+key, text);
	if(typeof rel == "object") {
		document.getElementById(key+"result").innerText = JSON.stringify(rel);
	} else {
		document.getElementById(key+"result").innerText = rel;
	}
}


//发送post请求
function post(method, data) {
	var result = {};
	$.ajax({
		url : method,
		data : data,
		type : 'post',
		cache : false,
		dataType : 'json',
		async : false,
		success : function(data) {
			result = data;
		},
		error : function(data) {
			if (data.status != 200)
				alert("请求异常:" + data.responseText);
		}
	});
	return result;
}
</script>
</html>